package com.gerfmarquez.creadormusica.utilities;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.util.Log;

public class Utilities {
	private final static String TAG = "Utilities";
	
	private static Utilities utility;
	private Utilities() {
		
	}
	
	public static Utilities getInstance() {
		if (utility == null) {
			utility = new Utilities();
		}
		return utility;
	}
	
	public  byte[] getBytesFromFile(String filename, Context context) {
		InputStream stream = null;
    	BufferedInputStream bis = null;
    	DataInputStream dis = null;
    	
    	byte[] fileBytes = null;
    	
        try {
        	
        	stream = context.getAssets().open(filename);
        	bis = new BufferedInputStream(stream);
        	dis = new DataInputStream(bis);
        	
        	Log.d(TAG, "El numero total de bytes de este archivo cpiano es " + stream.available());
//        	sonidoWav = new byte[stream.available()];
        	fileBytes = new byte[Constants.ONE_SEC_WAV_BYTES];
        	for(int i=0; i < Constants.ONE_SEC_WAV_BYTES; i++) {
        		fileBytes[i] = dis.readByte();
        	}
        	Log.v(TAG, "File read");
//        	dis.read(fileBytes);
        	
        } catch(IOException io) {
        	Log.e(TAG, "No se pudo abrir el archivo");
        	fileBytes = null;
        }
        finally {
        	closeStream(stream);
        	closeStream(bis);
        	closeStream(dis);
        }
        return fileBytes;
	}
    public static  long getTenThousandPerSec() {
    	return System.nanoTime()/100000;
    }
	
	private static void closeStream(InputStream is) {
    	try {
    		if (is != null)
    			is.close();
    	} catch(IOException io) {
    	}
	}
}
